package com.ruoyi.fapp.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.fapp.domain.AppChatMsg;
import com.ruoyi.fapp.domain.AppMessage;
import com.ruoyi.fapp.mapper.AppChatMsgMapper;
import com.ruoyi.fapp.model.WsInfo;
import com.ruoyi.fapp.service.AppChatMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AppChatMsgServiceImpl  implements AppChatMsgService {


    @Autowired
    private AppChatMsgMapper appChatMsgMapper;

    @Override
    public List<AppChatMsg> getChatList(Integer userId) {
        //List<AppChatMsg> chatList = appChatMsgMapper.getChatList(userId);
        return  appChatMsgMapper.getChatList(userId);



    }

    @Override
    public void deleteChat(String chatId) {

    }

    @Override
    public void getChatContent(String chatId) {

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addChat(WsInfo wsInfo) {






        // 添加聊天数据
        AppMessage message = wsInfo.getMessage();

        message.setMessageId(IdUtil.randomUUID());
        message.setCreateTime(DateUtils.getNowDate());
        message.setUpdateTime(DateUtils.getNowDate());
        if (message.getConversionId() == null){

            message.setConversionId(IdUtils.randomUUID());
        }

        appChatMsgMapper.addChat(message);


        //更新会话列表
        AppChatMsg appChatMsg = new AppChatMsg();
        appChatMsg.setConversationId(message.getConversionId());
        appChatMsg.setSenderId(Integer.parseInt(wsInfo.getSenderId()));
        appChatMsg.setReceiverId(Integer.parseInt(wsInfo.getReceptIid()));

        appChatMsgMapper.updateConversion(appChatMsg);


    }
}
